package com.lgx.mobile.rest.user;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.exception.DataException;
import com.lgx.common.service.IDictionaryService;
import com.lgx.common.web.util.DictUtils;
import com.lgx.right.entity.User;
import com.lgx.right.entity.UserExpand;
import com.lgx.right.service.IUserExpandService;
import com.lgx.right.service.IUserService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/updateDegree")
public class UpdateDegreeController {
	
	private static final Logger logger = Logger.getLogger(UpdateDegreeController.class);

	@Autowired
	IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}
	
	@Autowired
	IUserExpandService userExpandService;
	
	public IUserExpandService getUserExpandService() {
		return userExpandService;
	}
	
	@Autowired
	IDictionaryService dictionaryService;

	public IDictionaryService getDictionaryService() {
		return dictionaryService;
	}

	/**
	 * 更新会员等级
	 * @param userId id is key
	 * @return 0：失败；1：成功；2：用户不存在；3：参数异常，有参数为空；4：没有该用户的扩展信息
	 */
	@RequestMapping(value="/{id}",method=RequestMethod.GET)
	@ResponseBody
	public Object updateDegree(@PathVariable("id") String userId){
		if(StringUtils.isNotBlank(userId)){
			User user = getUserService().findUserById(Long.parseLong(userId));
			if(user!=null){
				UserExpand userExpand = getUserExpandService().findUserExpandByUserId(Long.parseLong(userId));
				if(userExpand != null){
					String status = "0";
					//会员等级
					List<Dictionary> userDegree = getDictionaryService().findDictionaryList(DictUtils.USER_DEGREE);
					String degree = "";
					for(Dictionary dic : userDegree){
						if(user.getCard().getScore() >= Double.parseDouble(dic.getCode())){
							degree = dic.getName();
						}
					}
					userExpand.setDegree(degree);
					try {
						getUserExpandService().updateUserExpand(userExpand);
						status = "1";
					} catch (DataException e) {
						e.printStackTrace();
					}
					return status;
				}else{
					if(logger.isDebugEnabled()){
						logger.debug("updateDegree faild userId is error");
					}
					return "4";
				}
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("updateDegree faild userId is error");
				}
//				"updateDegree faild please check userId is exists";
				return "2";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("updateDegree faild userId is not filled");
			}
//		    "updateDegree faild please check userId is filled";
			return "3";
		}
	}
}
